package com.fullstack.controller;

import com.databases.dao.ClothesMapper;
import com.fullstack.bean.Clothes;
import com.fullstack.bean.Clothes_type;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ClothesController {

    public List<Clothes> selectallclothes(HttpServletRequest request){
        String sex;

        //注入值
        sex = request.getParameter("sex");

        List<Clothes> clothes = null;



        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象

            ClothesMapper mapper = session.getMapper(ClothesMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            clothes = mapper.select_allmen(sex);

            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }




        return clothes;
    }

//    返回所有的衣服品牌
    public List<String> select_allclothesbrand(HttpServletRequest request){
        List<String> brandlist= null;
        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象

            ClothesMapper mapper = session.getMapper(ClothesMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            brandlist = mapper.select_allclothesbrand();

            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(brandlist);


        return brandlist;
    }

    //    返回最大价格
    public int select_maxmoney(HttpServletRequest request){
        int maxmoney=0;
        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象

            ClothesMapper mapper = session.getMapper(ClothesMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            maxmoney = mapper.select_maxmoney();

            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();


        } catch (IOException e) {
            e.printStackTrace();
        }



        return maxmoney;
    }

    //    对衣服进行条件查询
    public List<Clothes> select_dreamclothes(HttpServletRequest request){
        List<Clothes> list = null;
        Clothes_type clothes_type= new Clothes_type();


//        clothes_type.setMoney_max(request.getParameter(null));
//        clothes_type.setMoney_min(request.getParameter(null));


//        clothes_type.setId(Integer.parseInt(request.getParameter("form[id]")));
        clothes_type.setBrand(request.getParameter("form[brand]"));
        clothes_type.setType(request.getParameter("form[resource]"));
        clothes_type.setMoney_max(Integer.parseInt(request.getParameter("form[money][1]")));
        clothes_type.setMoney_min(Integer.parseInt(request.getParameter("form[money][0]")));

        System.out.println(clothes_type.getId());
        System.out.println(clothes_type.getBrand());
        System.out.println(clothes_type.getType());
        System.out.println(clothes_type.getMoney_max());
        System.out.println(clothes_type.getMoney_min());
        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象

            ClothesMapper mapper = session.getMapper(ClothesMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            list = mapper.select_dreamclothes(clothes_type);

            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
        for(Clothes c : list){
            System.out.println(c.toString());
        }



        return list;
    }
}
